package com.zh.note.huawei.onehundred.斗地主1最长的顺子;

import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;


public class Main {
    static String[] mGraph = {"3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A"};

    public static void main(String[] args) {
        try {
            Scanner sc = new Scanner(System.in);
            String myValue = sc.nextLine();
            String overValue = sc.nextLine();
            Map<String, Integer> map = new HashMap<>();
            for (String s : mGraph) {
                map.put(s, 4);
            }
            valueDiffer(map, myValue);
            valueDiffer(map, overValue);
            String res = findChain(map);
            System.out.println(res);


        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("input error");
        }
    }

    /**
     * 从map中查找符合条件的顺子
     *
     * @param map
     * @return
     */
    private static String findChain(Map<String, Integer> map) {
        String res = "N0-CHAIN";
        int left = 0;
        int right = 0;
        for (int i = 0; i < mGraph.length; i++) {
            String card = mGraph[i];
            if (map.get(card) > 0) {
                left = i;
                while (i < mGraph.length - 1 && map.get(mGraph[i + 1]) > 0) {
                    i++;
                }
                right = i + 1;
            }
        }
        if (right - left >= 5) {
            StringBuilder sb = new StringBuilder();
            for (int i = left; i < right; i++) {
                sb.append(mGraph[i]).append("-");
            }
            // 去掉末尾的-
            if (sb.length() > 0) {
                res = sb.substring(0, sb.length() - 1);
            }
        }
        return res;

    }

    //map减去出现的次数
    private static void valueDiffer(Map<String, Integer> map, String myValue) {
        String[] split = myValue.split("-");
        for (String card : split) {
            if (map.containsKey(card)) {
                map.put(card, map.get(card) - 1);
            }
        }
    }

}